package cn.mumzone.pos.ui.widgets;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import cn.mumzone.basic.util.DisplayUtil;

/**
 * Created By Dlx
 * on 2019/7/4
 * 自定义三角箭头view
 * 只用于预约界面对齐那个错位的时间线
 */
public class TrianglePointView extends View {

    private boolean isDrawingTriangle = false;
    private float mHeight, mWidth;
    private float triHeight, triWidth;
    private float percent = 90;
    private float offset = DisplayUtil.dip2px(getContext(), 20);
    private float offsetPercent;
    private boolean isEnd = false;

    public TrianglePointView(Context context) {
        super(context);
    }

    public TrianglePointView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public TrianglePointView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mHeight = getHeight();
        mWidth = getWidth();
        triWidth = DisplayUtil.dip2px(getContext(), 13);
        triHeight = mHeight;
        offsetPercent = offset / mWidth;

    }

    public float getPercent() {
        return percent;
    }

    public void setDrawingTriangle(boolean tof) {
        isDrawingTriangle = tof;
    }

    public float setPercent(float percent) {
        if (percent > 0) {
            isDrawingTriangle = true;
        }
        float overDrawingPercent = 0;
        if (percent > 100) {
            overDrawingPercent = percent - 100f;
        }
        this.percent = percent;
        invalidate();
        return overDrawingPercent;
    }

    public void setIsEnd(boolean tof) {
        isEnd = tof;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawTriangle(canvas);
    }

    public void drawTriangle(Canvas canvas) {
        if (!isDrawingTriangle)
            return;
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setColor(Color.parseColor("#F44E4E"));
        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(triWidth, 0);
        path.lineTo(triWidth / 2, triHeight);
        path.close();
        canvas.save();
        float currentPositionX = (((mWidth + triWidth - (isEnd ? offset : 0)) * percent / 100) - (triWidth));
        canvas.translate(currentPositionX, 0);
        canvas.drawPath(path, p);
        canvas.restore();
        if (currentPositionX > mWidth) {
            isDrawingTriangle = false;
        }
    }

}
